
package Servlets;

import DAO.DAO_Nivel;
import Interfaces.Inter_Nivel;
import Model.Nivel;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class ServletNivel extends HttpServlet
{

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception 
 {
    
       Inter_Nivel dao =new DAO_Nivel();
        Nivel nivel = new Nivel();

        String cmd =request.getParameter("cmd");
        
       if(cmd==null){
          cmd ="erro";
      }
       
       if(cmd!=null && !cmd.equals("erro"))
      {
          try{
              nivel.setDES_Nivel(request.getParameter("txtdescricao"));
              }
           catch(Exception e){
              throw new Exception("Erro em Nivel classe ServletNivel \n"+e.getMessage());
          }
       }
       
       try
        {
          RequestDispatcher rd =null;

          if(cmd.equals("listar")) //Mostrar usuarios
          {
              List nivelList = dao.ListaNivel();
              request.setAttribute("nivelList",nivelList);
              rd =request.getRequestDispatcher("");
          }

          else if(cmd.equals("gravar"))
          {
              dao.GravaNivel(nivel);
              rd =request.getRequestDispatcher("/ServletNivel?cmd=listar");
          }

          else if(cmd.equals("excluir"))
          {
              dao.ExcluiNivel(nivel);
              rd =request.getRequestDispatcher("/ServletNivel?cmd=listar");
          }

          else if(cmd.equals("alterar"))
          {
              nivel =dao.LocalizaNivel(nivel.getID_Nivel());
              HttpSession session = request.getSession(true);
              session.setAttribute("nivel",nivel);
              rd =request.getRequestDispatcher("");
          }

          else if(cmd.equals("atualizar"))
          {
              dao.AlteraNivel(nivel);
              rd =request.getRequestDispatcher("/ServletNivel?cmd =listar");
          }
          else if( cmd.equalsIgnoreCase( "principal" ) )
          {
                rd = request.getRequestDispatcher( "/index.jsp" );
          }
            rd.forward(request, response);
      }
      catch(Exception e)
        {
            e.printStackTrace( );
            throw new ServletException( e );
        }
    }
 
 @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)    
      throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ServletUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
  /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ServletUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
     /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
 
 
        
}//fecha a classe ServletNivel

   
    

   

